package com.yw.pms.service;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.yw.pms.dao.ProblemDao;
import com.yw.pms.dao.common.Page;
import com.yw.pms.dao.entity.Problem;
import com.yw.pms.utils.CommonResp;

@Service
public class ProblemService {
	
	private final static Logger logger = LoggerFactory.getLogger(ProblemService.class);
	
	@Resource
	ProblemDao problemDao;

	public CommonResp<Boolean> addProblem(Problem problem) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			problemDao.insert(problem);
			commonResp.success();
//			logger.info("操作员【{}】新增问题【{}】成功",problem.getOperatorName(),problem);
		} catch (Exception e) {
			logger.error("新增问题发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> delete(String problemId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			Integer id = Integer.valueOf(problemId);
			problemDao.delete(id);
			commonResp.success();
			logger.info("删除问题【{}】成功",problemId);
		} catch (Exception e) {
			logger.error("删除问题发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> update(Problem problem) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			problemDao.update(problem);
			commonResp.success();
//			logger.info("操作员【{}】更新问题信息【{}】成功",problem.getOperatorName(),problem);
		} catch (Exception e) {
			logger.error("修改问题发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<List<Problem>> queryProblem(Problem problem) {
		problem.setPage(new Page(problem.getPageNo()));
		CommonResp<List<Problem>> commonResp = new CommonResp<List<Problem>>();
		try {
			List<Problem> listPage = problemDao.listPage(problem);
			commonResp.success();
			commonResp.setData(listPage);
			Map<String,Object> extraData = Maps.newHashMapWithExpectedSize(1);
			extraData.put("page", problem.getPage());
			commonResp.setExtraData(extraData);
//			logger.info("操作员【{}】查询问题信息【{}】成功",problem.getOperatorName(),commonResp);
		} catch (Exception e) {
			logger.error("分页查询问题列表发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Problem> getProblemById(String problemId) {
		CommonResp<Problem> commonResp = new CommonResp<Problem>();
		if(Strings.isNullOrEmpty(problemId)) {
			commonResp.fail();
			commonResp.setMsg("问题ID不可传空");
			return commonResp;
		}
		try {
			Problem problem = problemDao.getById(Integer.valueOf(problemId));
			commonResp.success();
			commonResp.setData(problem);
//			logger.info("操作员【{}】查询问题信息【{}】成功",problem.getOperatorName(),commonResp);
		} catch (Exception e) {
			logger.error("根据主键获取问题信息发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
}
